package org.ntu.csn.doraemon.canvas;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import org.ntu.csn.doraemon.data.RMSUtil;
import org.ntu.csn.doraemon.model.Mark;


public class SaveForm implements CommandListener {

	private Form saveForm = new Form("Insert you message");
	
	private ResultForm ff;
	
	private TextField tf;
	
	private RankingForm rf;
	
	private StartMenu sm;
	
	Mark mark1 = new Mark();

	public SaveForm(ResultForm ff,StartMenu sm) {
		this.ff = ff;
		this.sm = sm;
		save();
	}
	
	private void save() {
		saveForm.append("Fill your message"); 
		tf = new TextField("Name","", 10, TextField.ANY);
		saveForm.append(tf);
		StringItem item = new StringItem("Score", String.valueOf(ff.getMark().getMark()));
		saveForm.append(item);
		saveForm.addCommand(Listener.ok);
		saveForm.setCommandListener(this);
		saveForm.addCommand(Listener.back);
		saveForm.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d) {
		if ( c == Listener.ok) {
			
			mark1.setName(this.tf.getString());
System.out.print(ff == null);
			mark1.setMark(ff.getMark().getMark());
			mark1.setTime("ddd");
			RMSUtil.insertObject(mark1); 
			rf = new RankingForm(sm);
		  Display.getDisplay(sm).setCurrent(rf.getRankingForm());
      } else if ( c == Listener.back) {
    	  Display.getDisplay(sm).setCurrent(StartMenu.mainMenu);
      }
	}

	public Form getSaveForm() {
		return saveForm;
	}

	public void setSaveForm(Form saveForm) {
		this.saveForm = saveForm;
	}
	
	
}
